// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command

/**
 * 更新公告信息
 */
exports.main = async (event, context) => {
  const { id, operation } = event

  if (operation === 'close') {
    // 关闭公告
    return await db.collection('notice')
      .doc(id)
      .update({
        data: {
          status: 0
        }
      })
  } else if (operation === 'image') {
    // 更换图片
    return await db.collection('notice')
      .doc(id)
      .update({
        data: {
          status: 2,
          tosee: 1
        }
      })
  } else if (operation === 'detailImages') {
    // 更换详情图片
    return await db.collection('notice')
      .doc(id)
      .update({
        data: {
          tosee: 0
        }
      })
  }
}